import http from '../utils/http';
export const getAllUsers = async () => {
  try {
    const response = await http.get('/users');
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 分页获取用户列表
 * @param {number} pageNum - 当前页码
 * @param {number} pageSize - 每页数量
 * @returns {Promise<AjaxResult>} 返回分页用户数据
 */
export const getUsersByPage = async (pageNum = 1, pageSize = 10) => {
  try {
    const response = await http.get(`/users/page?pageNum=${pageNum}&pageSize=${pageSize}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 条件分页查询用户
 * @param {Object} params - 查询参数
 * @param {number} params.pageNum - 当前页码
 * @param {number} params.pageSize - 每页数量
 * @param {Object} params.condition - 查询条件
 * @param {string} [params.condition.username] - 用户名
 * @param {string} [params.condition.realName] - 真实姓名
 * @param {number} [params.condition.userType] - 用户类型
 * @param {string} [params.condition.company] - 公司
 * @param {string} [params.condition.phone] - 手机号码
 * @param {number} [params.condition.status] - 用户状态
 * @returns {Promise<AjaxResult>} 返回分页用户数据
 */
export const getUsersByCondition = async (params) => {
  const { pageNum = 1, pageSize = 10, condition = {} } = params;
  try {
    // 打印请求参数，便于调试
    console.log('条件查询参数:', { pageNum, pageSize, condition });
    
    // 确保userType和status是数字类型
    if (condition.userType !== undefined && condition.userType !== '') {
      condition.userType = Number(condition.userType);
    }
    
    if (condition.status !== undefined && condition.status !== '') {
      condition.status = Number(condition.status);
    }
    
    const response = await http.post(`/users/page/condition?pageNum=${pageNum}&pageSize=${pageSize}`, condition);
    return response.data;
  } catch (error) {
    console.error('条件查询出错:', error);
    throw error;
  }
};

export const getUserById = async (id) => {
  try {
    const response = await http.get(`/users/${id}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const createUser = async (userData) => {
  try {
    const response = await http.post('/users', userData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const updateUser = async (id, userData) => {
  try {
    const response = await http.put(`/users/${id}`, userData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const deleteUser = async (id) => {
  try {
    const response = await http.delete(`/users/${id}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据用户名查找用户ID
 * @param {string} username - 用户名
 * @returns {Promise<AjaxResult<List<Map<String, Object>>>>} 返回匹配的用户ID列表
 */
export const searchUserIdsByUsername = async (username) => {
  try {
    console.log('Searching user IDs for username:', username);
    const response = await http.get(`/users/search/userid?username=${encodeURIComponent(username)}`);
    console.log('User ID search response:', response.data);
    return response.data;
  } catch (error) {
    console.error('搜索用户ID出错:', error);
    throw error;
  }
}; 